package com.monitor.dao.impl;

import org.hibernate.SessionFactory;

import com.monitor.dao.SettingDao;
import com.monitor.po.Setting;
import com.monitor.util.CommenUtil;

public class SettingDaoImpl implements SettingDao {
	private SessionFactory sessionFactory;

	@Override
	public String getValue(String key, String defaultVal) throws Exception {
		try {
			Setting setting = (Setting) sessionFactory.getCurrentSession().get(Setting.class, key);
			String returnVal = setting == null || CommenUtil.isEmpty(setting.getValue()) ? defaultVal : setting.getValue();
			return returnVal;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting SettingDao.getValue()....");
		}
	}

	@Override
	public void updateSetting(Setting setting) throws Exception {
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(setting);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting SettingDao.updateSetting()....");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
